package com.ebupt.migu.music.iop.mapper;

import com.ebupt.migu.music.iop.pojo.RowData;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Result;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.PropertyAccessorFactory;
import org.springframework.data.hadoop.hbase.RowMapper;

import java.util.List;

/**
 * @author tz
 */
@Slf4j
public class RowDataMapper implements RowMapper<RowData> {

    @Override
    public RowData mapRow(Result result, int rowNum) {
        RowData pojo = new RowData();
        BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(pojo);
        List<Cell> ceList = result.listCells();
        if (ceList != null && ceList.size() > 0) {
            for (Cell c : ceList) {
                String cellName = new String(CellUtil.cloneQualifier(c));
                if (!"class".equals(cellName)) {
                    beanWrapper.setPropertyValue(cellName, new String(CellUtil.cloneValue(c)).split(","));
                }
            }
            return pojo;
        } else {
            return null;
        }
    }
}
